package com.school.movie.common.utils;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.VideoSize;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

/**
 * 图片工具
 *
 * @author swift
 */
@Component
@Slf4j
public class MediaUtil {

    @Value("${upload.absPath}")
    public void setImagePath(String imagePath) {
        MediaUtil.IMAGE_PATH = imagePath;
    }

    private static String IMAGE_PATH;
    private static final String EMPTY_EXTEND_NAME = "";
    private static final String DEFAULT_VIDEO_EXTEND_NAME = ".mp4";
    private static final String DEFAULT_IMAG_EXTEND_NAME = ".jpg";
    private static final String DEFAULT_RESOLUTION = "1920x1080";
    private static final Encoder VIDEO_ENCODER = new Encoder();

    private MediaUtil() {
    }

    private static Map<String, Object> videoTypes = new HashMap<>();

    static {
        videoTypes.put(".mp4", 1);
        videoTypes.put(".wav", 2);
    }

    public static boolean isImage(InputStream is) {
        try {
            BufferedImage read = ImageIO.read(is);
            return read != null;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean isVideo(MultipartFile file) {
        if (videoTypes.containsKey(getExtendName(file))) {
            return true;
        } else {
            return readFileContentType();
        }
    }

    private static boolean readFileContentType() {
        //todo 读取真实的文件格式,更安全的校验
        return false;
    }

    /**
     * 返回扩展名，如.jpg
     * @param file
     * @return
     */
    public static String getExtendName(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        if (Objects.nonNull(originalFilename)) {
            int i = originalFilename.lastIndexOf('.');
            if (i != -1) {
                return originalFilename.substring(i);
            }
        }
        return EMPTY_EXTEND_NAME;
    }


    public static String generateVideoCoverImage(String filePath) {
        String resolution = DEFAULT_RESOLUTION;
        try {
            VideoSize videoSize = VIDEO_ENCODER.getInfo(new File(filePath)).getVideo().getSize();
            resolution = videoSize.getHeight() + "x" + videoSize.getWidth();
        } catch (EncoderException e) {
            e.printStackTrace();
            log.error("解析视频宽高出现错误,现已采用默认宽高裁剪视频!");
        }

        String imageName = UUID.randomUUID().toString() + DEFAULT_IMAG_EXTEND_NAME;
        String absoluteImagePath = IMAGE_PATH + imageName;
        FfmpegUtil.generateVideoCoverImage(filePath, absoluteImagePath, "0", resolution);
        return "card/" + imageName;
    }


}
